package com.jinghua.treatment.controller;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.controller.BaseController;
import com.jinghua.treatment.common.domain.JSONResult;
import com.jinghua.treatment.entity.*;
import com.jinghua.treatment.entity.vo.PicEntry;
import com.jinghua.treatment.service.*;
import com.jinghua.treatment.tools.QiNiuUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * 问诊单记录
 * Created by 王兴光 on 2018/5/24.
 */
@Controller
@RequestMapping(value = "/inquiry")
public class SysInquiryAnswersController extends BaseController {

    @Resource
    private SysInquiryAnswersService inquiryAnswersService;
    @Resource
    private SysInquirySubjectCategoryService inquirySubjectCategoryService;
    @Resource
    private SysInquiryAnswersSubService inquiryAnswersSubService;
    @Resource
    private SysPatientService patientService;
    @Resource
    private InquiryPicService inquiryPicService;

    @ApiOperation(value = "获取医生所属的问诊单数量")
    @RequestMapping(value = "/getCountByDoctorId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getCount(Integer doctorId){
        if(doctorId==null || doctorId == 0)
            doctorId = 39;  // 模拟数据
        if (super.session.getAttribute("currentDoctor")!=null){
            doctorId = ((SysDoctor)super.session.getAttribute("currentDoctor")).getDoctorId();
        }
        int count =  inquiryAnswersService.countByDoctor(doctorId);

        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取问诊单数量",count) ;
    }

    @ApiOperation(value = "获取医生所属的问诊单列表")
    @RequestMapping(value = "/getListByDoctorId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getInquiryList(Integer doctorId){
        if(doctorId==null || doctorId == 0)
            doctorId = 39;  // 模拟数据
        if (super.session.getAttribute("currentDoctor")!=null){
            doctorId = ((SysDoctor)super.session.getAttribute("currentDoctor")).getDoctorId();
        }
        SysInquiryAnswersExample example = new SysInquiryAnswersExample();
        SysInquiryAnswersExample.Criteria criteria = example.createCriteria();
        example.getOredCriteria().add(criteria.andDoctorIdEqualTo(doctorId));
        // 根据医生编号，获取问诊单列表
        List<SysInquiryAnswers> inquirys = inquiryAnswersService.selectByExample(example);

        // 获取一级分类列表
        SysInquirySubjectCategoryExample categoryExample = new SysInquirySubjectCategoryExample();
        categoryExample.getOredCriteria().add(categoryExample.createCriteria().andParentCodeIsNull());
        List<SysInquirySubjectCategory> categoryList = inquirySubjectCategoryService.selectByExample(categoryExample);

        // 获取当前医生问诊单列表的子级记录
        List<SysInquiryAnswersSub> subs = inquiryAnswersSubService.selectByDoctorId(doctorId);
        // 定义存储三级分类编号的列表
        List<String> answersList = new ArrayList<String>();
        // 迭代问诊单子级记录
        for (SysInquiryAnswersSub sub : subs){             // {{"010101,010102,010103"},{"010201,010204"}...}
            String[] answers = sub.getAnswers().split(",");// {"010101","010102","010103"}
            for(String answer : answers){                  // "010101"
                answersList.add(answer);    // 将迭代到的第三级分类的编号存储在列表中
            }
        }

        // 根据第三级分类编号获取分类详情列表
        List<SysInquirySubjectCategory> categoryList2 = inquirySubjectCategoryService.selectByAnswers(answersList);

        // 定义存储多个列表的复杂结果类型
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("inquirys",inquirys);
        resultMap.put("categoryList",categoryList);
        resultMap.put("categoryList2",categoryList2);
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取问诊单列表",resultMap);
    }

    @ApiOperation(value = "添加问诊单信息")
    @RequestMapping(value = "/createInquiry",method = RequestMethod.POST)
    public String addAnswers(SysInquiryAnswers answers, MultipartFile[] tongue, MultipartFile[] faceImg,MultipartFile[] woundImg){
        List<InquiryPic> inquiryPicList = new ArrayList<InquiryPic>();   // 问诊单文件列表
        QiNiuUtil  qiNiuUtil  = new QiNiuUtil("inquiry");
        //处理图片信息
        List<PicEntry> tongueList = new ArrayList<>();
        List<PicEntry> faceImgList = new ArrayList<>();
        List<PicEntry> woundImgList = new ArrayList<>();
        try {
            if(tongue!=null&&tongue.length>0){
                for(MultipartFile tongueFile :tongue){
                    String fileName = qiNiuUtil.upload2QiNiu(tongueFile.getBytes(),"");
                    tongueList.add(new PicEntry("tongue",fileName));
                    InquiryPic inquiryPic = new InquiryPic();
                    inquiryPic.setPicPath(fileName);
                    inquiryPic.setPicType("tongue");
                    inquiryPicList.add(inquiryPic);
                }
            }
            if(faceImg!=null&&faceImg.length>0){
                for(MultipartFile faceImgFile :tongue){
                    String fileName = qiNiuUtil.upload2QiNiu(faceImgFile.getBytes(),"");
                    faceImgList.add(new PicEntry("faceImg",fileName));
                    InquiryPic inquiryPic = new InquiryPic();
                    inquiryPic.setPicPath(fileName);
                    inquiryPic.setPicType("faceImg");
                    inquiryPicList.add(inquiryPic);
                }
            }
            if(woundImg!=null&&woundImg.length>0){
                for(MultipartFile tongueFile :woundImg){
                    String woundImgFile = qiNiuUtil.upload2QiNiu(tongueFile.getBytes(),"");
                    woundImgList.add(new PicEntry("woundImg",woundImgFile));
                    InquiryPic inquiryPic = new InquiryPic();
                    inquiryPic.setPicPath(woundImgFile);
                    inquiryPic.setPicType("woundImg");
                    inquiryPicList.add(inquiryPic);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        answers.setCreateDate(new Date());
        // 生成随机数
        //int inquiryId = (int)(Math.random()*1000);
        if(answers.getInquiryId()==null || "".equals(answers.getInquiryId())){
            answers.setInquiryId("ss");
        }

        inquiryAnswersService.insertSelective(answers);
        session.setAttribute("tongueList",tongueList);
        session.setAttribute("faceImgList",faceImgList);
        session.setAttribute("woundImgList",woundImgList);
        // 添加问诊单文件信息
        inquiryPicService.insert(inquiryPicList,answers.getInquiryId());
        super.session.removeAttribute("templateList");  // 移除问诊单模板列表
        super.session.setAttribute("inquiryId",answers.getInquiryId());
        return "redirect:/mod-page/inquiry/inquiry-for-send-next.html";
    }

    @ApiOperation(value = "根据患者编号，查询问诊单列表")
    @RequestMapping(value = "/selectByPatientId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult selectByPatientId(Integer patientId){
        List<SysInquiryAnswers> inquiryAnswersList = inquiryAnswersService.selectByPatientId(patientId);
        for (int i = 0 ; i< inquiryAnswersList.size();i++){
            inquiryAnswersList.get(i).setPatient(patientService.selectById(patientId));
        }
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"获取患者的问诊单列表",inquiryAnswersList);
    }

    @ApiOperation(value = "添加问诊单分类信息")
    @RequestMapping(value = "/addAnswersItem",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult addAnswersItem(String radioValue,String checkboxValue,String textValue){
        Integer inquiryId = 2;
        if(super.session.getAttribute("inquiryId")!=null){
            inquiryId = (Integer)super.session.getAttribute("inquiryId");
        }
        System.out.println("textValue"+textValue);
        inquiryAnswersSubService.insertSelective(radioValue,checkboxValue,textValue,inquiryId);
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"添加问诊单分类信息成功","");
    }

    @ApiOperation(value = "根据问诊单编号获取问诊单信息")
    @RequestMapping(value = "/getListByInquiryId",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult getListByInquiryId(String inquiryId){
        System.out.println("inquiryId："+inquiryId);
        SysInquiryAnswers inquiry = inquiryAnswersService.selectByPrimaryKey(Integer.valueOf(inquiryId));
        inquiry.setPatient(patientService.selectById(inquiry.getPatientId()));
        List<SysInquirySubjectCategory> templateDetailList = null;
        // 获取分类模板详情列表
        if(super.session.getAttribute("templateDetailList")==null){
            templateDetailList = inquirySubjectCategoryService.selectTemplateByInquiryId(inquiry.getInquiryId());
            super.session.setAttribute("templateDetailList",templateDetailList);
        }else{
            templateDetailList = (List<SysInquirySubjectCategory>)super.session.getAttribute("templateDetailList");
        }
        inquiry.setDetailList(templateDetailList);  // 添加问诊单模板列表

        inquiry.setSubList(inquirySubjectCategoryService.selectSubListByInquiryId(inquiryId));
        return new JSONResult(GlobalConstant.SUCCESS_CODE,"查询问诊单信息成功",inquiry);
    }

}
